<?php

namespace App\Http\Controllers\Wap;

use App\Http\Requests;
use App\Model\Category;
use App\Model\Member;
use App\Model\Picture;
use App\Model\Product;
use App\Model\PromoteDerate;
use App\Model\PromoteGive;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class ProductController extends Controller
{

    public function __construct(Request $request){
        $this->request = $request;
        $this->openid = session('wechat_user')['id'];
        $this->limit = 6;
        if (!empty($this->openid)) {
            $this->memberId = Member::where('openid',$this->openid)->pluck('id');
        }
    }

    /**
     * @param $id
     * @return $this
     * 商品详情
     * czs
     */
    public function detail($id){
        if (empty($this->memberId)) {
            $memberId = 0;
        } else {
            $memberId = $this->memberId;
        }
        $one = Product::selGoods($id)->getData();
        if ($one->is_derate==1) {
            $derate = PromoteDerate::get();
        }
        if ($one->is_give==1) {
            $give = PromoteGive::get();
        }
        $picIds = explode(",",$one->pic_ids);
        $pics = Picture::whereIn('id',$picIds)->select('middle_img')->get();
        return view('wap.goods.detail')
            ->with('one',$one)
            ->with('derate',!empty($derate)?$derate:"")
            ->with('give',!empty($give)?$give:"")
            ->with('pics',$pics)
            ->with('memberId',$memberId);
    }

    /**
     * @return mixed
     * 商品列表
     * czs
     */
    public function index(){
        if (empty($this->memberId)) {
            $memberId = 0;
        } else {
            $memberId = $this->memberId;
        }
        $parentId = $this->request->input('id');
        $res = Product::productList($parentId,$this->limit)->getData();
        $menu = Category::parentList()->getData();
        return view('wap.goods.list')
            ->with('menu',$menu)
			->with('id',$parentId)
			->with('more',$res->hasMorePages()?1:0)
            ->with('res',$res)
            ->with('memberId',$memberId);
    }

	public function pullajax() {
        $id = $this->request->input('id');
        $list = Product::productList($id,$this->limit)->getData();
        $html = view('wap.goods.list-ajax')->with('res',$list)->render();
        $data = [
            'html'=>$html,
            'more'=>$list->hasMorePages()?1:0
        ];
        return $data;
    }

}
